package com.example.basic.web.service.basicData.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.basic.base.impl.BaseEntityServiceImpl;
import com.example.basic.exception.ServiceException;
import com.example.basic.utils.PinyinUtils;
import com.example.basic.utils.StringUtils;
import com.example.basic.utils.WubiUtils;
import com.example.basic.web.enums.DataCodeEnum;
import com.example.basic.web.mapper.basicData.BasicDataMapper;
import com.example.basic.web.model.basicData.BasicData;
import com.example.basic.web.service.basicData.BasicDataService;
import com.example.basic.web.service.basicDataValue.BasicDataValueService;
import com.example.basic.web.service.dataCode.DataCodeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.example.basic.utils.CheckUtil.notNull;


@Service
public class BasicDataServiceImpl extends BaseEntityServiceImpl<BasicDataMapper, BasicData> implements BasicDataService {

    @Resource
    private BasicDataMapper basicDataMapper;

    @Resource
    private DataCodeService dataCodeService;

    @Resource
    private BasicDataValueService basicDataValueService;


    /**
     * 新增或更新
     * @param basicData
     * @return
     */
    @Override
    public BasicData addOrUpdate(BasicData basicData) {
        notNull(basicData, "参数不能为空");
        notNull(basicData.getName(),"名称不能为空");

        //编码不为空，修改信息
        if(StringUtils.notEmptyParams(basicData.getCode())){
            //校验名称唯一
            LambdaQueryWrapper<BasicData> lqw = new LambdaQueryWrapper<>();
            lqw.eq(BasicData::getName,basicData.getName());
            lqw.ne(BasicData::getCode,basicData.getCode());
            BasicData localData = this.mapper.selectOne(lqw);
            if(localData != null){
                throw new ServiceException("名称已存在");
            }
            preUpdate(basicData);
        }else {
            //校验名称唯一
            LambdaQueryWrapper<BasicData> lqw = new LambdaQueryWrapper<>();
            lqw.eq(BasicData::getName,basicData.getName());
            BasicData localData = this.mapper.selectOne(lqw);
            if(localData != null){
                throw new ServiceException("名称已存在");
            }

            preCreate(basicData);
            //生成编码
            String basicDataCode = dataCodeService.generateDataCodeNo(DataCodeEnum.BASIC_DATA_CODE.getType());
            basicData.setCode(basicDataCode);
        }

        //生成拼音码五笔码
        generatePyWbCode(basicData);

        save(basicData);

        return basicData;
    }

    /**
     * @create 生成五笔码、拼音码
     * @param basicData 模型实体类
     */
    private void generatePyWbCode(BasicData basicData) {
        // 获取名称
        String name = StringUtils.safeToString(basicData.getName(), "");
        // 生成拼音码
        if("".equals(StringUtils.safeToString(basicData.getPyCode(),"").replaceAll(" ",""))) {
            String userNamePinyin = PinyinUtils.getSimplePinYin(name);
            basicData.setPyCode(userNamePinyin);
        }
        // 生成拼音码
        if("".equals(StringUtils.safeToString(basicData.getWbCode(),"").replaceAll(" ",""))) {
            String userNameWubi = WubiUtils.getSimpleWuBi(name);
            basicData.setWbCode(userNameWubi);
        }
    }

    /**
     * 删除
     * @param code 编码
     * @return
     */
    @Override
    public Boolean remove(String code) {
        notNull(code,"编码不能为空");

        LambdaQueryWrapper<BasicData> lqw = new LambdaQueryWrapper<>();
        lqw.eq(BasicData::getCode,code);
        BasicData localData = this.mapper.selectOne(lqw);

        if(localData == null){
            throw new ServiceException("未获取到需要删除的配置信息");
        }

        //删除配置配置内容
        basicDataValueService.removeByBasicCode(code);

        return basicDataMapper.remove(code);

    }


    /**
     * 配置列表
     * @param queryPage 分页
     * @param searchParam 名称
     * @return
     */
    @Override
    public IPage<BasicData> queryList(Page<BasicData> queryPage, String searchParam) {

        LambdaQueryWrapper<BasicData> lqw = new LambdaQueryWrapper<>();

        if(StringUtils.notEmptyParams(searchParam)){
            lqw.like(BasicData::getName,searchParam)
                    .or().like(BasicData::getPyCode,searchParam)
                    .or().like(BasicData::getWbCode,searchParam);
        }
        lqw.orderByAsc(BasicData::getCode);
        return this.mapper.selectPage(queryPage,lqw);
    }

    /**
     * 配置信息详情
     * @param code 配置编码
     * @return
     */
    @Override
    public BasicData detail(String code) {

        notNull(code,"配置编码不能为空");

        LambdaQueryWrapper<BasicData> lqw = new LambdaQueryWrapper<>();
        lqw.eq(BasicData::getCode,code);
        return  this.mapper.selectOne(lqw);
    }
}
